package com.example.myapplication2;

import java.nio.*;
import java.nio.channels.*;
import java.util.*;
import java.lang.*;
import java.net.InetSocketAddress;

public class testconn {
    myBuf recBuf = new myBuf(), msgBuf = new myBuf();
    long staTime = System.currentTimeMillis();
    public testconn(LinkedList<Integer> ports)
            throws Exception {
        cli a = new cli(ports,new logClass());
        myThr aa = new myThr();
        aa.a = a;
        aa.setDaemon(true);
        aa.start();
        SocketChannel dataSock = SocketChannel.open();
        InetSocketAddress add = new InetSocketAddress("localhost", a.dataPort);
        dataSock.connect(add);
        SocketChannel numSock = SocketChannel.open();
        InetSocketAddress add2 = new InetSocketAddress("localhost", a.numPort);
        numSock.connect(add2);
        dataSock.configureBlocking(false);
        numSock.configureBlocking(false);
        int cacheSize = 1024 * 200;
        while (true) {
            Selector selector = Selector.open();
            dataSock.register(selector, SelectionKey.OP_READ);
            numSock.register(selector, SelectionKey.OP_READ);
            int ret = selector.select(100);
            Iterator selectedKeys = selector.selectedKeys().iterator();
            while (selectedKeys.hasNext()) {
                SelectionKey key = (SelectionKey) selectedKeys.next();
                selectedKeys.remove();
                if (!key.isValid()) {
                    continue;
                }
                SelectableChannel channel = key.channel();
                if (channel == numSock && key.isReadable()) {
                    byte[] xx = helpFunc.tcpRead(numSock);
                    recBuf.append(xx);
                    while (recBuf.size() >= 4) {
                        cacheSize += recBuf.readDWord();
                    }
                }
                if (channel == dataSock && key.isReadable()) {
                    byte[] xx = helpFunc.tcpRead(dataSock);
                    msgBuf.append(xx);
                    dealData();
                }
            }
            selector.close();
            dataSock.configureBlocking(true);
            while (cacheSize > 0) {
                myBuf w = new myBuf(1004);
                w.writeDWord(1004);
                byte[] b = new byte[1000];
                w.append(b);
                helpFunc.tcpWrite(dataSock, w.get());
                cacheSize -= 1004;
            }
            dataSock.configureBlocking(false);
        }
    }
    ArrayList<byte[]> dealData() {
        ArrayList<byte[]> msgList = new ArrayList<byte[]>();
        while (true) {
            if (msgBuf.size() < 4)
                break;
            int ll = msgBuf.readDWord();

            if (msgBuf.size() < ll - 4) {
                msgBuf.startPos -= 4;
                break;
            }
            msgList.add(msgBuf.readArr(ll - 4));
            helpFunc.print("get msg "+ll);
        }
        return msgList;
    }
    public static void main(String[] args) throws Exception {
        LinkedList<Integer> ports = new LinkedList<Integer>();
        for (int i = 10000; i < 10010; i++) {
            ports.add(i);
        }
        testconn cc = new testconn(ports);
    }
}
